package com.sunjob.mromessage.service;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.constant.MROConstant;
import com.sunjob.common.domain.loginServer.SysMenu;
import com.sunjob.common.domain.loginServer.SysRole;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.message.SysNotice;
import com.sunjob.common.domain.message.SysNoticeReceive;
import com.sunjob.common.domain.message.SysNoticeSelector;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mromessage.mapper.MroNoticeMapper;
import com.sunjob.mromessage.mapper.MroNoticeReceiveMapper;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-05 15:03
 */
@Service
public class MroMessageServiceImpl implements MroMessageService {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Autowired
    MroNoticeMapper mroNoticeMapper;
    @Autowired
    MroNoticeReceiveMapper mroNoticeReceiveMapper;

    @Override
    public boolean pushNoticeBySelector(SysNoticeSelector sysNoticeSelector) {
        if (sysNoticeSelector.getSysNotice() == null) {
            return false;
        }
        String name = null;
        try {
            SaSession tokenSession = StpUtil.getTokenSession();
            SysUser userInfo = (SysUser) tokenSession.get("userInfo");
            name = userInfo.getLoginName();
        } catch (Exception e) {
            return false;
        }
        SysNotice sysNotice = sysNoticeSelector.getSysNotice();
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime updateTime = createTime;

        Long noticeId = MROUtils.nextId();
        sysNotice.setNoticeId(noticeId);
        sysNotice.setCreateTime(createTime);
        sysNotice.setCreateBy(name);
        sysNotice.setUpdateBy(name);
        sysNotice.setUpdateTime(updateTime);
        List<SysNoticeReceive> sysNoticeReceiveList = new ArrayList<>();
        if (sysNoticeSelector.isSelectAll()) {
            SysNoticeReceive sysNoticeReceive = new SysNoticeReceive();
            sysNoticeReceive.setReceiveId(-1l);
            sysNoticeReceive.setCreateBy(name);
            sysNoticeReceive.setCreateTime(createTime);
            sysNoticeReceive.setUpdateBy(name);
            sysNoticeReceive.setUpdateTime(updateTime);
            sysNoticeReceive.setReceiveNoticeId(MROUtils.nextId());
            sysNoticeReceive.setStatus(0);
            sysNoticeReceive.setNoticeId(noticeId);  //
            sysNoticeReceiveList.add(sysNoticeReceive);
        } else {
            if (sysNoticeSelector.getIds().size() == 0) {
                return false;
            }
            Integer type = sysNoticeSelector.getType();
            for (Long id : sysNoticeSelector.getIds()) {
                SysNoticeReceive sysNoticeReceive = new SysNoticeReceive();
                sysNoticeReceive.setNoticeId(noticeId);
                sysNoticeReceive.setReceiveId(id);
                sysNoticeReceive.setReceiveType(type);
                sysNoticeReceive.setReceiveNoticeId(MROUtils.nextId());
                sysNoticeReceive.setCreateBy(name);
                sysNoticeReceive.setCreateTime(createTime);
                sysNoticeReceive.setUpdateBy(name);
                sysNoticeReceive.setUpdateTime(updateTime);
                sysNoticeReceive.setStatus(0);
            }
        }
        mroNoticeMapper.saveNotice(sysNotice);
        mroNoticeReceiveMapper.saveNoticeReceive(sysNoticeReceiveList);
        return true;
    }

    @Override
    public List<SysNotice> pullNoticeByUserId(Long userId,Integer type) {
        List<SysRole> roleListByUserId = getRoleListByUserId(userId);
        SysUser userInfo = (SysUser)StpUtil.getTokenSession().get("userInfo");
        Map<String,Object> map= new HashMap();
        map.put("depId",userInfo.getDeptId());
        List<Long> ids = roleListByUserId.stream().map(e->e.getRoleId()).collect(Collectors.toList());
        map.put("roles",ids);
        map.put("type",type);
        return mroNoticeMapper.pullNoticeByUserId(map);
    }

    @Override
    public List<SysRole> getRoleListByUserId(Long userId) {
        HashOperations<String, String, List<String>> hashOperations = redisTemplate.opsForHash();
        HashOperations<String, String, SysRole> hashOperations1 = redisTemplate.opsForHash();
        // 获取角色列表id
        List<String> roles = hashOperations.get(MROConstant.USER_ROLES, userId + "");
        List<SysRole> res = new ArrayList<>();
        roles.stream().forEach(e -> {
                    SysRole sysRole = hashOperations1.get(MROConstant.ROLES, e);
                    res.add(sysRole);
                }
        );
        return res;
    }

    @Override
    public List<SysMenu> getMenuListByUserId(Long userId) {
        HashOperations<String, String, List<String>> hashOperations = redisTemplate.opsForHash();
        HashOperations<String,String,SysMenu> hashOperations1 = redisTemplate.opsForHash();
        List<SysRole> roleListByUserId = getRoleListByUserId(userId);
        List<String> meunIds = new ArrayList<>();
        roleListByUserId.stream().forEach(e->{
            meunIds.addAll(hashOperations.get(MROConstant.ROLE_MENUS,e.getRoleId()+""));
        });
        List<SysMenu> res= new ArrayList<>();
        meunIds.stream().forEach(e->{
            res.add(hashOperations1.get(MROConstant.MENUS,e));
        });
        return res;
    }


    //以下，没有进行权限检查

    @Override
    public List<SysNotice> findAllNotice() {
        return mroNoticeMapper.findAllNotice();
    }

    @Override
    public List<SysNotice> findAllAnnouncement() {
        return mroNoticeMapper.findAllAnnouncement();
    }

    @Override
    public List<SysNotice> findAllInform() {
        return mroNoticeMapper.findAllInform();
    }

    @Override
    public SysNotice queryAnnouncementOrInformById(long notice_id) {
        return mroNoticeMapper.queryAnnouncementOrInformById(notice_id);
    }

    @Override
    public List<SysNotice> queryAnnouncementOrInformByCreator(String Creator) {
        return mroNoticeMapper.queryAnnouncementOrInformByCreator(Creator);
    }

    @Override
    public List<SysNotice> queryNoticeBy(SysNotice sysNotice) {
        return mroNoticeMapper.queryNoticeBy(sysNotice);
    }

    @Override
    public void updateNotice(SysNotice sysNotice) {
        mroNoticeMapper.updateNotice(sysNotice);
    }

    @Override
    public void  deleteNoticeById(Long noticeId) {
        mroNoticeMapper.deleteNoticeById(noticeId);
        mroNoticeReceiveMapper.deleteNoticeByNoticeId(noticeId);
    }

    @Override
    public void deleteNoticeByReceiveNoticeId(Long receiveNoticeId) {
        mroNoticeReceiveMapper.deleteNoticeByReceiveNoticeId(receiveNoticeId);
    }

    @Override
    public void updateNoticeReceiveBy(SysNoticeReceive sysNoticeReceive) {
        mroNoticeReceiveMapper.updateNoticeReceiveBy(sysNoticeReceive);
    }


}